package server

import (
	"github.com/akirakazu/go-simplejson"
)

// "find" command handler
func (s *Server) onFind(id string, m *Pack) {
	// Check existence for pattern: key
	if !m.Msg.Has("key") {
		msg := json.New()
		msg.Set("err", "key missing")
		s.response(id, m, msg)
		return
	}

	key := m.Msg.Get("key")

	// Find all conns matching given pattern
	conns := s.findConns(key, s.conns[id].identity)
	res := []*json.Json{}
	for _, v := range conns {
		res = append(res, s.conns[v].identity)
	}

	// Response with conn's identities
	msg := json.New()
	msg.Set("clients", res)
	s.response(id, m, msg)
}
